﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using MedicalEquipmentAccounting.CustomControls.Views;
using MedicalEquipmentAccounting.CustomControls;
using System.Threading;
using System.Globalization;
using BusinessLogicLayer;

namespace MedicalEquipmentAccounting
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            MessageBox.Show(Engine.test().Count.ToString());
        }

        private void TabPageMouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Middle)
            {
                var tabpage = (ExtendedTabPage)sender;
                if (tabpage != null)
                {
                    mainTabControl.TabPages.Remove(tabpage);
                    //tabpage.CloseTab();
                }
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            mainTabControl.SelectedTab.Refresh();
        }

        private void AddTab<T>(string tabName) where T : new()
        {
            try
            {
                ExtendedTabPage tabpage = new ExtendedTabPage();
                tabpage.Text = tabName;
                
                tabpage.MouseWheel += tabpage_MouseWheel;
                var control = new T();

                tabpage.Controls.Add(control as UserControl);

                mainTabControl.TabPages.Add(tabpage);
                mainTabControl.SelectedTab = tabpage;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogException(ex, "Can`t create tab of type " + typeof(T).ToString());
            }
        }

        void tabpage_MouseWheel(object sender, MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void MenuClick(object sender, EventArgs e)
        {
            try
            {
                var menuToolStripItem = (ToolStripMenuItem)sender;
                var menuItem = (MenuItems)Enum.Parse(typeof(MenuItems), menuToolStripItem.Name.Replace("ToolStripMenuItem", ""));

                switch (menuItem)
                {
                    case MenuItems.AddDocument: AddTab<DocumentEditView>("Add Document");
                        break;
                    case MenuItems.AddDocumentName: AddTab<DocumentNameEditView>("Add Document Name");
                        break;
                    case MenuItems.AddItem: AddTab<ItemsEditView>("Add items");
                        break;
                    case MenuItems.AddNorms: AddTab<NormEditView>("Test");
                        break;
                    case MenuItems.ShowAllItems: AddTab<ItemsGridView>("All items");
                        break;
                    default: break;
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.LogException(ex, "Can`t resolve MenuItem Click.");
            }
        }

        private void languageSwitch_Click(object sender, EventArgs e)
        {
            var menu_item = sender as ToolStripMenuItem;
            foreach (ToolStripMenuItem item in languageToolStripMenuItem.DropDown.Items)
            {
                item.Checked = false;
            }
            menu_item.Checked = true;

            switch (menu_item.Name.Replace("ToolStripMenuItem", ""))
            {
                case "english":
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                    break;
                case "ukrainian":
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("uk-UA");
                    break;
                case "russian":
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
                    break;
                default:
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("uk-UA");
                    break;
            };
            foreach (Control item in mainTabControl.SelectedTab.Controls[0].Controls)
            {
                item.Refresh();
            }
        }
    }
}
